
"""
    pycoopiler - a compiler developed for PCS2056 course (Compilers) at Poli-USP
    Copyright (C) 2008 Andre Felipe Santos    
    Copyright (C) 2008 Rodrigo Rodrigues da Silva

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
"""

from pycoopiler.token import *
from pycoopiler.enum import enum
from pycoopiler.submaquina import submaquina

class maq_chfunc(submaquina):
    
    states = enum(" _0_OPEN_RB \
                    _2_CONST_OU_ID \
                    _5_CLOSE_RB_OU_COMMA \
                    _6 \
                    _8_CONST \
                    _9_CLOSE_SB \
                    _10_CLOSE_RB_OU_COMMA \
                    _20_FIM")
    
    def __init__(self, _tkzr):
        self.tkzr = _tkzr     
        self.state = self.states._0_OPEN_RB
        self.name = "CHFUNC"
        self.log_chamada(self.name)
        self.current = self.tkzr.current
        
    def analyze(self):
        
        while not (self.state == self.states._20_FIM):
            
            self.log_state()  
              
            if self.state == self.states._0_OPEN_RB:
                if self.current.type == self.current.types.OPEN_RB:
                    self.state = self.states._2_CONST_OU_ID
                else: raise Exception ("Erro Sintatico! Esperando OPEN_RB")
                self.consume()
            
            elif self.state == self.states._2_CONST_OU_ID:
                if self.current.type == self.current.types.CONST:
                    self.state = self.states._5_CLOSE_RB_OU_COMMA
                elif self.current.type == self.current.types.ID:
                    self.state = self.states._6
                elif self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._20_FIM
                else: raise Exception ("Erro Sintatico! Esperando CONST ou ID ou CLOSE_RB")
                self.consume()
            
            elif self.state == self.states._5_CLOSE_RB_OU_COMMA:
                if self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._20_FIM
                elif self.current.type == self.current.types.COMMA:
                    self.state = self.states._2_CONST_OU_ID
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_RB ou COMMA")
                self.consume()
            
            elif self.state == self.states._6:
                if self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._20_FIM
                elif self.current.type == self.current.types.COMMA:
                    self.state = self.states._2_CONST_OU_ID
                elif self.current.type == self.current.types.OPEN_SB:
                    self.state = self.states._8_CONST
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_RB ou COMMA ou OPEN_SB")
                self.consume()               
            
            elif self.state == self.states._8_CONST:
                if self.current.type == self.current.types.CONST:
                    self.state = self.states._9_CLOSE_SB
                else: raise Exception ("Erro Sintatico! Esperando CONST")
                self.consume()
            
            elif self.state == self.states._9_CLOSE_SB:
                if self.current.type == self.current.types.CLOSE_SB:
                    self.state = self.states._10_CLOSE_RB_OU_COMMA
                else: raise Exception ("Erro Sintatico! Esperando CLOSE_SB")
                self.consume()
            
            elif self.state == self.states._10_CLOSE_RB_OU_COMMA:
                if self.current.type == self.current.types.CLOSE_RB:
                    self.state = self.states._20_FIM
                elif self.current.type == self.current.types.COMMA:
                    self.state = self.states._2_CONST_OU_ID
                else: raise Exception ("Erro Sintatico! Esperando 1CLOSE_RB ou COMMA")
                self.consume()
            
            else: raise Exception("ERRO: Estado invalido: " + self.state)
        
        self.log_retorno(self.name)